package com.qq.aidoctor.tools;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qq.aidoctor.bean.Appointment;
import com.qq.aidoctor.bean.DoctorSchedule;
import com.qq.aidoctor.common.BaseResponse;
import com.qq.aidoctor.common.ResultUtils;
import com.qq.aidoctor.service.AppointmentService;
import com.qq.aidoctor.service.DoctorScheduleService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author Bob
 * @Date：2025/6/9 10:09
 * @description：
 */
@Component
@Deprecated
public class AppointmentTools_depreccated {
    @Resource
    private AppointmentService appointmentService;

    @Resource
    private DoctorScheduleService doctorScheduleService;

    @Tool(name="预约挂号", value = "根据参数，先执行工具方法queryDepartment查询是否可预约，并直接给\n" +
            "用户回答是否可预约，并让用户确认所有预约信息，用户确认后再进行预约。如果用户没有提供具体的医生姓名，请从\n" +
            "向量存储中找到一位医生。")
    public String bookAppointment(Appointment appointment) {
//查找数据库中是否包含对应的预约记录
        Appointment appointmentDB = appointmentService.getOne(appointment);
        if (appointmentDB == null) {
            appointment.setId(null);//防止大模型幻觉设置了id
            if (appointmentService.save(appointment)) {
                LambdaUpdateWrapper<DoctorSchedule> updateWrapper = Wrappers.lambdaUpdate(DoctorSchedule.class)
                        .eq(DoctorSchedule::getDoctorName, appointment.getDoctorName())
                        .eq(DoctorSchedule::getDate, appointment.getDate())
                        .eq(DoctorSchedule::getTime, appointment.getTime())
                        .setDecrBy(DoctorSchedule::getAvailableSlots, 1);
                doctorScheduleService.update(updateWrapper);
                return "预约成功";
            } else {
                return "预约失败";
            }
        }
        return "您在相同的科室和时间已有预约";
    }
    @Tool(name = "取消预约挂号", value = "当用户想要取消预约挂号时，调用此工具类进行取消挂号")
    public String cancelAppointment(Appointment appointment) {
        // 模拟数据库查询，判断用户是否已经预约过该时间段
        Appointment appointmentDB = appointmentService.getOne(appointment);
        if (appointmentDB != null) {
            // 模拟数据库插入预约信息
            if(appointmentService.removeById(appointmentDB.getId()))
                return "取消成功";
            return "系统繁忙，请稍后再试。";
        }
        return "您没有预约记录，请核对信息。";
    }

    @Tool(name = "查询是否有号源", value="根据科室名称，日期，时间和医生查询是否有号源，并返回给用户。如果用户指定了医生，但是该医生已经预约满了，则查看是否有其他医生，如果有则返回给用户。")
    public BaseResponse<Object> queryDepartment(
            @P(value = "科室名称") String name,
            @P(value = "日期") String date,
            @P(value = "时间，可选值：上午、下午") String time,
            @P(value = "医生名称", required = false) String doctorName
    ) {
        // 根据科室名称、日期、时间段查询当前有哪些医生有排班且有号可挂
        List<DoctorSchedule> listAvailableDoctors = doctorScheduleService.listAvailableDoctors(name, date, time);
        if(listAvailableDoctors.isEmpty()){
            return ResultUtils.success(null,"没有可预约的医生");
        }
        //如果用户指定了医生，则查看该医生是否在可预约的医生列表中
        if(doctorName != null){
            DoctorSchedule doctorSchedule = listAvailableDoctors.stream()
                    .filter(dc -> dc.getDoctorName().equals(doctorName))
                    .findFirst()
                    .orElse(null);
            //如果医生在可预约的医生列表中，则返回给用户
            if(doctorSchedule != null){
                return ResultUtils.success(doctorSchedule,"用户可以预约指定医生");
            }
        }
        return ResultUtils.success(listAvailableDoctors,"用户指定的医生没有排班了，将其他医生返回给用户");
    }


}
